package com.xb.testdown.data.http;



import com.xb.testdown.common.config.StringVaule;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * @作者：ws
 * @时间：2020/10/29
 * @描述：HttpManager 封装Retrofit2 和 OkHttp3
 */
public class HttpManager {

    // 初始化 OkHttpClient
    public OkHttpClient getOkHttpClient() {
        // log 用拦截器
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();

        // 开发模式记录整个 body ,否则只记录基本信息如返回 200, http协议版本等
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);

        return new OkHttpClient.Builder()
                //  HeadInterceptor 实现了 Interceptor ，用来往 Request  Header 中添加一些业务相关的数据数据，，如 APP 版本，token 信息
//                .addInterceptor(HeadInterceptor)
                .addInterceptor(logging)
                .connectTimeout(10, TimeUnit.SECONDS)
                .readTimeout(10, TimeUnit.SECONDS)
                .build();
    }

    public Retrofit getRetrofit(OkHttpClient okHttpClient) {
        Retrofit.Builder builder = new Retrofit.Builder()
                .baseUrl(StringVaule.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) // 添加 gson 转换器，默认将服务端返回的数据转化为 jAVABean 对象
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // RxJava2 的适配暂时未用到
                .client(okHttpClient);// 不调用的也会默认调用 okHttpClient,不过 okHttpClient 的参数都是默认的

        return builder.build();
    }
}
